package zj.it.bhne.bitmap;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView image;
    private Context context;
    private Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        initView();
    }

    private void initView() {
        Button suofang = findViewById(R.id.suofang);
        Button jianqie = findViewById(R.id.jianqie);
        Button xuanzhuan = findViewById(R.id.xuanzhuan);
        Button save = findViewById(R.id.save);
        image = findViewById(R.id.image);

        suofang.setOnClickListener(this);
        jianqie.setOnClickListener(this);
        xuanzhuan.setOnClickListener(this);
        save.setOnClickListener(this);

    }

    @SuppressLint("ShowToast")
    private void setToasts(String content) {
        if (toast == null) {
            toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
        } else {
            toast.setText(content);
        }
        toast.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.suofang:
                setToasts("缩放");
                Bitmap bitmap1 = MyUtils.readBitmapById(context, R.drawable.aaa);
                image.setImageBitmap(MyUtils.scaleImage(bitmap1, 500, 500));
                break;
            case R.id.jianqie:
                setToasts("剪切");
                Bitmap bitmap2 = MyUtils.readBitmapById(context, R.drawable.aaa);
                image.setImageBitmap(MyUtils.imageCrop(bitmap2, 1, 1, false));
                break;
            case R.id.xuanzhuan:
                setToasts("旋转");
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("输入旋转角度");
                final EditText editText = new EditText(context);
                editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                builder.setView(editText);
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if ("".equals(editText.getText().toString().trim()) || editText.getText().toString().trim() == null) {
                            setToasts("请输入角度");
                        } else {
                            if (Integer.parseInt(editText.getText().toString().trim()) < 1 || Integer.parseInt(editText.getText().toString().trim()) > 360)
                                setToasts("输入旋转角度错误");
                            else {
                                Bitmap bitmap3 = MyUtils.readBitmapById(context, R.drawable.aaa);
                                image.setImageBitmap(MyUtils.rotaingImageView(Integer.parseInt(editText.getText().toString().trim()), bitmap3));
                            }
                        }
                    }
                });
                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        setToasts("点击了取消");
                    }
                });
                builder.show();
                break;
            case R.id.save:
                setToasts("保存");
                Bitmap bitmap4 = MyUtils.readBitmapById(context, R.drawable.aaa);
                if (MyUtils.saveBmpToSd(Environment.getExternalStorageDirectory().getAbsolutePath() + "/aZhu/", bitmap4, "zhujiang.jpg", 1, false))
                    setToasts("保存成功");
                else
                    setToasts("保存失败");
                break;
        }
    }
}
